<?php
/*
 * Hàm format số cho dễ nhận diện bằng dấu phảy (,)
 * phân cách mức hàng nghìn.
 *
 * @author      huypv
 * @date        3/10/2009
 *
 * @param       int $value
 * @return      string
 */
function formatPrice($value) {
    $value = strval(intval($value));
    $temp = strrev($value);
    $len = strlen($temp);
    $ret = '';
    for ($i = 0; $i < $len; $i++) {
        $ret .= $temp[$i];
        if ($i % 3 == 2 && $i < $len - 1) $ret .= ',';
    }
    return strrev($ret);
}

/*
 * Co dau thanh khong dau
 */
function stripText($string)
{
    $from 	= array("à","ả","ã","á","ạ","ă","ằ","ẳ","ẵ","ắ","ặ","â","ầ","ẩ","ẫ","ấ","ậ","đ","è","ẻ","ẽ","é","ẹ","ê","ề","ể","ễ","ế","ệ","ì","ỉ","ĩ","í","ị","ò","ỏ","õ","ó","ọ","ô","ồ","ổ","ỗ","ố","ộ","ơ","ờ","ở","ỡ","ớ","ợ","ù","ủ","ũ","ú","ụ","ư","ừ","ử","ữ","ứ","ự","ỳ","ỷ","ỹ","ý","ỵ","À","Ả","Ã","Á","Ạ","Ă","Ằ","Ẳ","Ẵ","Ắ","Ặ","Â","Ầ","Ẩ","Ẫ","Ấ","Ậ","Đ","È","Ẻ","Ẽ","É","Ẹ","Ê","Ề","Ể","Ễ","Ế","Ệ","Ì","Ỉ","Ĩ","Í","Ị","Ò","Ỏ","Õ","Ó","Ọ","Ô","Ồ","Ổ","Ỗ","Ố","Ộ","Ơ","Ờ","Ở","Ỡ","Ớ","Ợ","Ù","Ủ","Ũ","Ú","Ụ","Ư","Ừ","Ử","Ữ","Ứ","Ự","Ỳ","Ỷ","Ỹ","Ý","Ỵ");
	$to 	= array("a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","d","e","e","e","e","e","e","e","e","e","e","e","i","i","i","i","i","o","o","o","o","o","o","o","o","o","o","o","o","o","o","o","o","o","u","u","u","u","u","u","u","u","u","u","u","y","y","y","y","y","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","D","E","E","E","E","E","E","E","E","E","E","E","I","I","I","I","I","O","O","O","O","O","O","O","O","O","O","O","O","O","O","O","O","O","U","U","U","U","U","U","U","U","U","U","U","Y","Y","Y","Y","Y");
    return str_replace($from, $to, $string);
}

/*
 * function make thumbnail of a channel
 * author      : Hieplt
 * created date: 10/08/2009
 */
function makeChannelThumbnail($channelID,$version = 1)
{
   //get config array
   $config = Vega_App::getInstance()->coreConfig;
   //return url
   return $config['site_url']."/".$config['static']['channel']."/"."$channelID.jpg?v=$version";
}

/*
 * function make thumbnail url of a category
 * author hieplt
 * created_date: 18/08/2009
 */
function makeChannelCategoryThumbnail($catID,$version)
{
   //get config array
   $config = Vega_App::getInstance()->coreConfig;
   //return url
   return $config['site_url']."/".$config['static']['category']."/"."$catID.jpg?v=$version";
}
/*
 * function makeVideoThumbnail($videoID,$version)
 * author      : hieplt
 * created time: 21-08-2009
 */
function makeVideoThumbnail($videoID,$version=1)
{
    return "http://static.m.clip.vn/0/0/0/".$videoID."/".$videoID."_large.jpg?v=".$version;
}
/*
 * function makeVODCategoryThumbnail($catID,$version)
 * desc         : tao thumbnail cua chuyen muc video
 * author       : hieplt
 * created time : 21-08-2009
 */
function makeVideoCategoryThumbnail($catID,$version)
{
   //get config array
   $config = Vega_App::getInstance()->coreConfig;
   //return url
   return $config['site_url']."/".$config['static']['vod_category']."/"."$catID.jpg?v=$version";
}

function getVideoThumbnailList($clipId)
{
    $config = Vega_App::getInstance()->coreConfig;
	$thumbs = array();

    $baseDir  = $config['static']['data_path'] . DS . $config['static']['video'] . storageSolutionEncode($clipId, true);
	$baseURL  = $config['static']['data_url'] . '/' . $config['static']['video'] . storageSolutionEncode($clipId) . '/' . $clipId;

		for($i=0; $i <= 9; $i++)
		{
			$thumbnailDir   = $baseDir.DS.$clipId."-000".$i.".jpg";
			$thumbnailURL   = "$baseURL/".$clipId."-000".$i.".jpg";
            $thumb['name']  = $clipId."-000".$i.".jpg";
            $thumb['url']   = $thumbnailURL;

			$thumbs[] = $thumb;
		}

	return $thumbs;
}

/**
 * storageSolutionEncode: tao ra duong dan toi thu muc chua file theo mo hinh phan cap thu muc moi
 * @param integer : id cua doi tuong, dung de ma hoa thu muc
 * @return string : duong dan theo mo hinh luu tru du lieu moi
 **/
function storageSolutionEncode($id)
{
	$step   		= 15;	//so bit de ma hoa ten thu muc tren 1 cap
	$layer  		= 3;	//so cap thu muc
	$max_bits 		= PHP_INT_SIZE*8;
	$result 		= "";

	for($i=$layer; $i>0; $i--)
	{
   		$shift   = $step*$i;
		$layer_name  = $shift<=$max_bits?$id >> $shift:0;

	   	$result .= "/".$layer_name;
	}

	return $result;
}

/****************************************************************
 * Function: copyAndResizeImage - Copy and Resize image file
 * @param String	- Name of file source
 * @param String	- Name of file dest
 * @param Integer	- Size of file dest in width
 * @param Integer	- Size of file dest in height
 * @return Bool		- True or False, true if success
 ****************************************************************/
function copyAndResizeImage($fileSource, $fileDest, $newWidth=0, $newHeight=0)
{
	if(!file_exists($fileSource))
	{
		return false;
	}
	$sourceFileInfo = getimagesize($fileSource);
	$width 	= $sourceFileInfo[0];
	$height = $sourceFileInfo[1];
	$mime 	= $sourceFileInfo["mime"];

    if(!$newWidth) $newWidth = $width;
    if(!$newHeight) $newHeight = $height;

	$xR 	= $newWidth/$width;
	$yR 	= $newHeight/$height;
	if ($xR < $yR)
	{
		$newHeight = floor($height*$xR);
	}
	elseif ($xR > $yR)
	{
		$newWidth = floor($width*$yR);
	}

	$thumbnail = imagecreatetruecolor($newWidth, $newHeight);
	switch ($mime)
	{
		case "image/jpeg":
			$source = imagecreatefromjpeg($fileSource);
			break;
		case "image/gif":
			$source = imagecreatefromgif($fileSource);
			break;
		case "image/png":
			$source = imagecreatefrompng($fileSource);
			break;
	}

	// Resize
	if(!imagecopyresized($thumbnail, $source, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height))
	{
		return false;
	}

	// Output
	if(!imagejpeg($thumbnail, $fileDest))	return false;
	if(!imagedestroy($source))				return false;
	return true;
}

/*
 * function convertToInterPhoneNumber
 */
function convertToInterPhoneNumber($number)
{
    if(substr($number, 0, 1) == '0')
    {
        $number = '84'.substr($number,1);
    }
    elseif(substr($number, 0, 2) != '84')
    {
        $number = "84$number";
    }
    return $number;
}